package pers.qianyu.month_202103.date_20210328;

import org.junit.Test;
import pers.qianyu.util.ListNodeUtil;
import pers.qianyu.util.model.ListNode;

/**
 * 82. 删除排序链表中的重复元素 II
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
 *
 * @author mizzle rain
 * @date 2021-03-28 14:35
 */
public class DeleteDuplicatesTwo {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        ListNode root = new ListNode(-1), p = root;
        for (ListNode cur = head; cur != null; ) {
            int v = cur.val;
            ListNode ne = cur.next;
            if (ne != null && ne.val == v) {
                while (ne != null && ne.val == v) ne = ne.next;
                cur = ne;
            } else {
                p.next = cur;
                p = p.next;

                ListNode t = cur.next;
                cur.next = null;
                cur = t;
            }
        }
        return root.next;
    }

    @Test
    public void test1() {
        ListNode root = ListNodeUtil.generateList(1, 2, 2);
        ListNode r = new DeleteDuplicatesTwo().deleteDuplicates(root);
        System.out.println(r);
    }
}
